#coding:utf8
import os
import sys
import re
#from matplotlib import pyplot as plt



class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[93m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'


fps = re.compile('Protocol:(.*):(.*)')
#fps = re.compile('ProtocolUDP:(.*):(.*)')



fn = sys.argv[1]

lines = open(fn).readlines()
fpss = []

num = 1
total = 1
xv = {}
nv = {}
maxSz = {}

avgSize = {}
for l in lines:
    ret = fps.findall(l)
    if len(ret) > 0:
       
        #print ret[0][0]
        pid = ret[0][0]
        size = ret[0][1]
        if pid not in xv:
            xv[pid] = 0
            nv[pid] = 0
            maxSz[pid] = [0, 9999999]
        xv[pid] += int(size)
        nv[pid] += 1
        sz = int(size)
        oldSz = maxSz[pid]
        maxSz[pid] = [max(oldSz[0], sz), min(oldSz[1], sz)]

#xv = nv

#报文的大小 数量 平均大小 最大大小
rows = []    
for p in xv:
    avgSize[p] = xv[p]/nv[p]
    rows.append([p, xv[p], avgSize[p], nv[p], maxSz[p]])



def cmp(a, b):
    c1 = a[1]
    c2 = b[1]
    if c1 > c2:
        return -1
    elif c1 < c2:
        return 1
    else:
        return 0


rows.sort(cmp=cmp)

l = len(rows)
width = 1.0/l

n = 0
sizes = []

ind = []
for r in rows:
    print r#, avgSize[r[0]], nv[r[0]]
    sizes.append(r[1])
    ind.append(n)
    n += 1

#plt.bar(ind, sizes)  
#plt.show()
